package space

import "src/zeus/iserver"

func NewInitParam(cap int, addition iserver.IInitParam) iserver.IInitParam {
	result := &InitParam{
		keyparam: make(map[string]interface{}),
	}
	if addition != nil {
		cap += addition.Num()
		for k, v := range addition.KeyParams() {
			result.keyparam[k] = v
		}
		// result.keyparam = addition.KeyParams()
	}
	result.param = make([]interface{}, 0, cap)
	return result
}

// InitParam Key 会用到的一些参数
const (
	// 地图/玩法模块枚举key
	PARAMKEY_SPACEMODEL = "GameplayModel"
	// 地图配置里的怪物数据
	PARAMKEY_SPACECONFIG = "SpaceConfig"
	// 怪物额外被动
	PARAMKEY_EXPASSIVE = "ExPassive"
	// 索敌范围
	PARAMKEY_SCANRANGE = "ScanRange"
	// 脱战范围
	PARAMKEY_OUTWARRANGE = "OutwarRange"
	// 战斗实体类型
	PARAMKEY_BATTLENTTTYPE = "BattleNTTType"
	// 阵营
	PARAMKEY_FACTION = "Faction"
	// 别名
	PARAMKEY_ALIAS = "Alias"
	// 词条
	PARAMKEY_ENTRYS = "Entrys"
	// AI类型
	PARAMKEY_AITYPE = "AIType"
	// AIIndex
	PARAMKEY_AIINDEX = "AIIndex"
	// EntityID
	PARAMKEY_ENTITYID = "EntityID"
	// 玩法标记位
	PARAMKEY_GAMEPLAY_STATE = "GamePlayState"
	// 召唤者
	PARAMKEY_SUMMON_ID = "SummonID"
	// 是否要同步仇恨
	PARAMKEY_SYNC_HATE = "SyncHate"
	// 朝向
	PARAMKEY_TOWARD = "Toward"
	// 配置表索引ID
	PARAMKEY_CONFIGID = "ConfigID"
	// 坐标
	PARAMKEY_POSITION = "Position"
)

type InitParam struct {
	param    []interface{}
	keyparam map[string]interface{}
}

func (s *InitParam) Params() []interface{} {
	return s.param
}

func (s *InitParam) KeyParams() map[string]interface{} {
	return s.keyparam
}

func (s *InitParam) Num() int {
	return len(s.param)
}

func (s *InitParam) Get(idx int) interface{} {
	if idx >= s.Num() {
		return nil
	}

	return s.param[idx]
}

func (s *InitParam) AddParam(p iserver.IInitParam) iserver.IInitParam {
	if p != nil && p.Num() >= 0 {
		s.param = append(s.param, p.Params()...)
		for key, v := range p.KeyParams() {
			s.SetVal(key, v)
		}
	}
	return s
}

func (s *InitParam) Add(p interface{}) iserver.IInitParam {
	if p != nil {
		s.param = append(s.param, p)
	}
	return s
}

func (s *InitParam) AddAll(pList []interface{}) iserver.IInitParam {
	if len(pList) > 0 {
		s.param = append(s.param, pList...)
	}
	return s
}

//visitor(index, param)
func (s *InitParam) ForeachParam(visitor func(int, interface{})) {
	for idx, p := range s.param {
		visitor(idx, p)
	}
}

// 根据key找值
func (this *InitParam) GetVal(key string) interface{} {
	v, ok := this.keyparam[key]
	if ok {
		return v
	} else {
		return nil
	}
}

// 设置key的值
func (this *InitParam) SetVal(key string, val interface{}) iserver.IInitParam {
	this.keyparam[key] = val
	return this
}

func NewInitArgs() (result *InitArgs) {
	result = new(InitArgs)
	result.param = make(map[string]interface{})
	return result
}

//初始化参数对象
type InitArgs struct {
	param map[string]interface{}
}

func (this *InitArgs) GetVal(key string) interface{} {
	v, ok := this.param[key]
	if ok {
		return v
	} else {
		return nil
	}
}

func (this *InitArgs) SetVal(key string, val interface{}) *InitArgs {
	this.param[key] = val
	return this
}
